//
//  GEWeekTimeFromatter.m
//  ERP
//
//  Created by goldeney on 15/4/2.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "GEWeekTimeFromatter.h"

@implementation GEWeekTimeFromatter

//获取今天的指定几天数据
+ (NSString*)getDayTimeString:(NSInteger)number
{
    NSDate *date = [NSDate date];
    NSDate *day = nil;
    if (number == 0) {
        day = date;
    } else {
        day = [NSDate dateWithTimeInterval:number*24*60*60 sinceDate:date];
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *oneDay = [dateFormatter stringFromDate:day];
    
    return oneDay;
}

//获取今天的指定月数据
+ (NSString*)getMonthTimeString:(NSInteger)number
{
    NSDate *date = [NSDate date];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //    NSDateComponents *comps = nil;
    //    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:0];
    [adcomps setMonth:number];
    [adcomps setDay:0];
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:date options:0];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSString *oneMonth = [dateFormatter stringFromDate:newdate];
    
    return oneMonth;
}

//获取今天的指定月数据
+ (NSString*)getYearTimeString:(NSInteger)number
{
    NSDate *date = [NSDate date];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //    NSDateComponents *comps = nil;
    //    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:number];
    [adcomps setMonth:0];
    [adcomps setDay:0];
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:date options:0];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy"];
    NSString *oneYear = [dateFormatter stringFromDate:newdate];
    
    return oneYear;
}

#pragma mark -

//获取今天的指定几天数据
+ (NSInteger)getOneDayTime:(NSInteger)number
{
    NSDate *date = [NSDate date];
    NSDate *day = nil;
    if (number == 0) {
        day = date;
    } else {
        day = [NSDate dateWithTimeInterval:number*24*60*60 sinceDate:date];
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd"];
    NSInteger oneDay =[[dateFormatter stringFromDate:day] integerValue];
    
    return oneDay;
}

//获取今天的指定月数据
+ (NSInteger)getOneMonthTime:(NSInteger)number
{
    NSDate *date = [NSDate date];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
//    NSDateComponents *comps = nil;
//    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:0];
    [adcomps setMonth:number];
    [adcomps setDay:0];
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:date options:0];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM"];
    NSInteger oneMonth = [[dateFormatter stringFromDate:newdate] integerValue];
    
    return oneMonth;
}

//获取前一周数据（字符串）
+ (NSArray*)getBeforeWeekTimeForString
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i=6; i>=0; i--) {
        NSInteger day = [self getOneDayTime:-i];
        [array addObject:[NSString stringWithFormat:@"%ld日",day]];
    }
    return array;
}

//获取半年数据（字符串）
+ (NSArray*)getBeforeSixMonthForString
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i=5; i>=0; i--) {
        NSInteger day = [self getOneMonthTime:-i];
        [array addObject:[NSString stringWithFormat:@"%ld月",day]];
    }
    return array;
}

#pragma mark - return NSString

+ (NSString*)getOneMonthTime:(NSInteger)number format:(NSString*)format
{
    NSDate *date = [NSDate date];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //    NSDateComponents *comps = nil;
    //    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:0];
    [adcomps setMonth:number];
    [adcomps setDay:0];
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:date options:0];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    NSString *oneMonth = [dateFormatter stringFromDate:newdate];
    
    return oneMonth;
}

//获取一年12个月数据（字符串）
+ (NSArray*)getBefore12MonthForString
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i=11; i>=0; i--) {
        NSString *month = [self getOneMonthTime:-i format:@"yyyy-MM"];
        [array addObject:[NSString stringWithFormat:@"%@",month]];
    }
    return array;
}

@end
